class Solution
{
public:
    int countStudents(vector<int> &students, vector<int> &sandwiches)
    {
        int n = students.size();
        int studentRemain = n;
        int i = 0;
        int j = 0;
        while (true)
        {
            int abandonCount = 0;
            while (students[i] != sandwiches[j])
            {
                if (students[i] != -1)
                {
                    ++abandonCount;
                }
                if (abandonCount == studentRemain)
                {
                    return studentRemain;
                }
                i = (i + 1) % n;
            }
            --studentRemain;
            if (studentRemain == 0)
            {
                return 0;
            }
            students[i] = -1;
            i = (i + 1) % n;
            ++j;
        }
        return 0;
    }
};